﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BSC_OTC_Management.BSC_SERVICES;
using System.DirectoryServices.AccountManagement;

namespace BSC_OTC_Management
{
    public partial class FrmLogin : Form
    {
        #region 
        Code.StaticVariable _code = new Code.StaticVariable();
        #endregion

        public FrmLogin()
        {
            InitializeComponent();
            this.txtPassword.KeyDown += new KeyEventHandler(txtPassword_KeyDown);
        }

        void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLogin_Click(sender, e);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (checkDieuKien() ==  true)
            {
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "BSC.COM.VN"))
                {
                    // validate the credentials
                    if (pc.ValidateCredentials(this.txtusername.Text, this.txtPassword.Text) == true)
                    {
                        SetPermission(txtusername.Text);
                        FrmMain _frmMain = new FrmMain();
                        this.Visible = false;
                        _frmMain.Show();
                    }
                    else
                    {
                        MessageBox.Show("Sai mật khẩu hoặc password!", Code.StaticVariable.messeseboxLuuY, MessageBoxButtons.OK);
                    }
                }
            }
        }

        private void SetPermission(string _e)
        {
            try
            {
                //username
                Code.StaticVariable.username = _e.ToUpper();
                // Lấy nhóm theo username
                DataTable _resut = _code._services.Get_Group_ByUserName(Code.StaticVariable.username);
                Code.StaticVariable.userID = _code._services.Get_UserID_ByUserName(Code.StaticVariable.username);
                //code chống lỗi Null data
                int i=0;
                Code.StaticVariable.groupName = new string[_resut.Rows.Count];
                Code.StaticVariable.groupID = new string[_resut.Rows.Count];
                foreach (DataRow row in _resut.Rows)
                {
                    Code.StaticVariable.groupName[i] = Convert.ToString(_resut.Rows[i]["grpname"]);
                    Code.StaticVariable.groupID[i] = Convert.ToString(_resut.Rows[i]["grpid"]);
                    i++;
                }
                
                // Lấy quyền menu theo nhóm
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private bool checkDieuKien()
        {
            if (this.txtusername.Text.Trim().Length==0)
            {
                MessageBox.Show("Chưa nhập username", Code.StaticVariable.messeseboxLuuY, MessageBoxButtons.OK);
                txtusername.Focus();
                return false;
            }
            if (this.txtPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("Chưa nhập password", Code.StaticVariable.messeseboxLuuY, MessageBoxButtons.OK);
                txtPassword.Focus();
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
